package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.RepairReportDTO;
import com.eastfair.venueservice.dto.RepairReportPageQuery;
import com.eastfair.venueservice.dto.RepairReportSaveDTO;
import com.eastfair.venueservice.dto.RepairReportUpdateDTO;
import com.eastfair.venueservice.entity.RepairReport;
import com.eastfair.venueservice.service.RepairReportService;
import com.eastfair.venueservice.vo.RepairReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 报修申请
 * </p>
 *
 * @author dqq
 * @date 2022-11-21
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/repairReport")
@Api(value = "RepairReport", tags = "报修申请")
@PreAuth(replace = "venueservice:repairReport:")
public class RepairReportController extends SuperController<RepairReportService, Long, RepairReport, RepairReportPageQuery, RepairReportSaveDTO, RepairReportUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private RepairReportService repairReportService;

    @Override
    public void handlerResult(IPage<RepairReport> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<RepairReport> repairReportList = list
                .stream()
                .map((map) -> RepairReport.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(repairReportList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<RepairReportPageQuery> params) {
        RepairReportPageQuery pageQuery = params.getModel();
    }

    /**
     * 查询报修申请列表VO.
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation("查询报修申请列表VO")
    @PostMapping("/query/repairReport")
    public R<Page<RepairReportVO>> queryRepairReport(@RequestBody PageParams<RepairReportDTO> pageParams) {
        log.info("queryRepairReport - 查询报修申请列表VO, pageParams={}", pageParams);
        Page<RepairReportVO> page = repairReportService.listRepairReportVO(pageParams);
        return R.successDef(page);
    }

    /**
     * 场馆查询报修申请列表VO.
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation("场馆查询报修申请列表VO")
    @PostMapping("/query/venue/repairReport")
    public R<Page<RepairReportVO>> queryRepairReportForVenue(@RequestBody PageParams<RepairReportDTO> pageParams) {
        log.info("queryRepairReportForVenue - 场馆查询报修申请列表VO, pageParams={}", pageParams);
        Page<RepairReportVO> page = repairReportService.listRepairReportVOForVenue(pageParams);
        return R.successDef(page);
    }

    /**
     * 报修单号获取报修申请VO.
     *
     * @param repairReportNumber the repair report number
     * @return the by report repair number
     */
    @ApiOperation("报修单号获取报修申请VO")
    @GetMapping("/get/{repairReportNumber}")
    public R<RepairReportVO> getByReportRepairNumber(@PathVariable("repairReportNumber") String repairReportNumber) {
        log.info("getByReportRepairNumber - 报修单号获取报修申请VO, repairReportNumber={}", repairReportNumber);
        RepairReportVO repairReportVO = repairReportService.getVOByReportRepairNumber(repairReportNumber);
        return R.successDef(repairReportVO);
    }

    /**
     * 保存报修申请.
     *
     * @param repairReportDTO the repair report dto
     * @return the r
     */
    @ApiOperation("保存报修申请")
    @SysLog("保存报修申请")
    @PostMapping("/save")
    public R<RepairReport> saveRepairReport(@RequestBody RepairReportDTO repairReportDTO) {
        log.info("saveRepairReport - 保存报修申请, repairReportDTO={}", repairReportDTO);
        try {
            return R.successDef(repairReportService.saveRepairReport(repairReportDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 更新报修申请.
     *
     * @param repairReportDTO the repair report dto
     * @return the r
     */
    @ApiOperation("更新报修申请")
    @SysLog("修改报修申请")
    @PostMapping("/update")
    public R<RepairReport> updateRepairReport(@RequestBody RepairReportDTO repairReportDTO) {
        log.info("updateRepairReport - 更新报修申请, repairReportDTO={}", repairReportDTO);
        return R.successDef(repairReportService.updateRepairReport(repairReportDTO));
    }

    /**
     * 取消报修申请.
     *
     * @param repairReportNumber the repair report number
     * @return the r
     */
    @ApiOperation("取消报修申请")
    @SysLog("取消报修申请")
    @GetMapping("/cancel/{repairReportNumber}")
    public R<Boolean> cancelRepairReport(@PathVariable("repairReportNumber") String repairReportNumber) {
        log.info("cancelRepairReport - 取消报馆申请={}", repairReportNumber);
        return R.successDef(repairReportService.cancelRepairReport(repairReportNumber));
    }

    /**
     * 更新报修申请为已完成.
     *
     * @param repairReportDTO the repair report dto
     * @return the r
     */
    @ApiOperation("更新报修申请为已完成")
    @SysLog("完成报修申请")
    @PostMapping("/complete")
    public R<Boolean> completeRepairReport(@RequestBody RepairReportDTO repairReportDTO) {
        log.info("completeRepairReport - 更新报修申请为已完成, repairReportDTO={}", repairReportDTO);
        try {
            return R.successDef(repairReportService.updateFinish(repairReportDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

}
